#include "wlscpthread.h"
#include "dcmtk/config/osconfig.h"

#include "dcmtk/dcmnet/dicom.h"
#include "dcmtk/ofstd/ofcmdln.h"
#include "dcmtk/dcmwlm/wltypdef.h"
#include "dcmtk/dcmdata/dcxfer.h"
#include "dcmtk/ofstd/ofconapp.h"
#include "dcmtk/dcmnet/assoc.h"
#include "dcmtk/dcmdata/cmdlnarg.h"
#include "dcmtk/dcmnet/dimse.h"
#include "dcmtk/dcmdata/dcvrat.h"
#include "dcmtk/dcmdata/dcvrlo.h"
#include "dcmtk/dcmwlm/wlds.h"
#include "dcmtk/dcmdata/dcsequen.h"
#include "dcmtk/dcmwlm/wldsfs.h"
#include "dcmtk/dcmwlm/wlmactmg.h"
#include "dcmtk/dcmnet/dimse.h"

WLScpThread::WLScpThread(const QString &_dbPath, int _port, QObject *parent) :
    dbPath(_dbPath),
    port(_port),
    QThread(parent)
{
}

void WLScpThread::run()
{
    WlmDataSourceFileSystem *dataSource = new WlmDataSourceFileSystem();
    dataSource->SetDfPath(dbPath.toLocal8Bit().data());

    OFCondition cond;

    // connect to data source
    cond = dataSource->ConnectToDataSource();
    if( cond.bad() ) {
        emit scpError(tr("Connect to data source failed, try another database folder."));
        delete dataSource;
        return;
    }

    // start providing the basic worklist management service
    WlmActivityManager *activityManager = new WlmActivityManager(
        dataSource, port,
        0,
        0,
        0, 0,
        ASC_DEFAULTMAXPDU, EXS_Unknown,
        1,
        1, 50,
        DIMSE_BLOCKING, 0, 30);

    cond = activityManager->StartProvidingService();
    if (cond.bad()) {
        emit scpError(tr("Start service failed."));
    }

    dataSource->DisconnectFromDataSource();
    delete activityManager;
    delete dataSource;
}
